from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse

from core.roles.role_check import check_perms
from libs.helper import DatetimeString, StrBoolNone
from libs.login import login_required
from controllers.console import api
from fields.online_mall.prop_fields_def import prod_list_fields,prod_fields
from services.online_mall.prod_prop_service import ProdPropService
from services.online_mall.product_service import ProductService

class ProductApi(Resource):
    @login_required
    @check_perms(perms=["prod:list"])
    @marshal_with(prod_list_fields)
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('id', type=str, required=False,location='args')
        parser.add_argument('name', type=str, required=False,location='args')
        parser.add_argument('shop_id', type=str, required=True,location='args')
        parser.add_argument('max_price', type=float, required=False,location='args')
        parser.add_argument('min_price', type=float, required=False,location='args')
        parser.add_argument('status', type=str, required=False,location='args')
        parser.add_argument('category_id', type=str, required=False,location='args')
        parser.add_argument('max_sold_num',type=int, required=False,location='args')
        parser.add_argument('min_sold_num',type=int, required=False,location='args')
        parser.add_argument('max_stocks',type=int, required=False,location='args')
        parser.add_argument('min_stocks',type=int, required=False,location='args')
        parser.add_argument('beg_putaway_time',type=DatetimeString("%Y-%m-%d %H:%M"),required=False,location='args')
        parser.add_argument('end_putaway_time',type=DatetimeString("%Y-%m-%d %H:%M"),required=False,location='args')
        parser.add_argument('hasUserPickUp',type=StrBoolNone,required=False,location='args')
        parser.add_argument('hasShopDelivery',type=StrBoolNone,required=False,location='args') 
        parser.add_argument('page',type=int,required=False,default=-1,location='args')
        parser.add_argument('page_size',type=int,required=False,default=-1,location='args')
        args = parser.parse_args()
        return ProductService.get_product_list(current_user,args)
        
    @login_required
    @check_perms(perms=["prod:create"])
    @marshal_with(prod_fields)
    def post(self):
        parser = reqparse.RequestParser()
        # parser.add_argument('id', type=str, required=False,location='json')
        parser.add_argument('name', type=str, required=True,location='json')
        parser.add_argument('shop_id', type=str, required=True,location='json')
        parser.add_argument('ori_price', type=float, required=False,location='json')
        parser.add_argument('price', type=float, required=False,location='json')
        parser.add_argument('brief', type=str, required=False,location='json')
        parser.add_argument('content', type=str, required=False,location='json')
        parser.add_argument('pic_file_id', type=str, required=False,location='json')
        parser.add_argument('imgs', type=str,action='append', required=False,location='json')
        parser.add_argument('status', type=str, required=False,location='json')
        parser.add_argument('category_id', type=str, required=False,location='json')
        # parser.add_argument('sold_num',type=int, required=False,location='json')
        # parser.add_argument('total_stocks',type=int, required=False,location='json')
        parser.add_argument('hasUserPickUp',type=StrBoolNone,required=False,location='json')
        parser.add_argument('hasShopDelivery',type=StrBoolNone,required=False,location='json')
        parser.add_argument('delivery_template_id', type=int, required=False,location='json')
        parser.add_argument('putaway_at',type=DatetimeString("%Y-%m-%d %H:%M"),required=False,location='json')
        parser.add_argument('sku_list',type=dict,action="append",required=False,location='json')
        args = parser.parse_args()
        return ProductService.create_product(current_user,args)
    
    @login_required
    @check_perms(perms=["prod:update"])
    @marshal_with(prod_fields)
    def put(self):
        parser = reqparse.RequestParser()
        parser.add_argument('id', type=str, required=False,location='json')
        parser.add_argument('name', type=str, required=True,location='json')
        parser.add_argument('shop_id', type=str, required=True,location='json')
        parser.add_argument('ori_price', type=float, required=False,location='json')
        parser.add_argument('price', type=float, required=False,location='json')
        parser.add_argument('brief', type=str, required=False,location='json')
        parser.add_argument('content', type=str, required=False,location='json')
        parser.add_argument('pic_file_id', type=str, required=False,location='json')
        parser.add_argument('imgs', type=str,action='append', required=False,location='json')
        parser.add_argument('status', type=str, required=False,location='json')
        parser.add_argument('category_id', type=str, required=False,location='json')
        parser.add_argument('sold_num',type=int, required=False,location='json')
        parser.add_argument('total_stocks',type=int, required=False,location='json')
        parser.add_argument('hasUserPickUp',type=StrBoolNone,required=False,location='json')
        parser.add_argument('hasShopDelivery',type=StrBoolNone,required=False,location='json')
        parser.add_argument('delivery_template_id', type=int, required=False,location='json')
        parser.add_argument('putaway_at',type=DatetimeString("%Y-%m-%d %H:%M"),required=False,location='json')
        args = parser.parse_args()
        return ProductService.update_product(current_user,args)
    
    
    @login_required
    @check_perms(perms=["prod:del"])
    def delete(self):
        parser = reqparse.RequestParser()
        parser.add_argument('id', type=str, required=True,location='json')
        args = parser.parse_args()
        prod_id = args['id']
        ProductService.delete_product(current_user,prod_id)
        return {"code": 200, "message": "success"}
    
api.add_resource(ProductApi, '/products')